// page/component/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      history: [],
      hot: ['电影', '动漫', '电视剧','小说'],
      name:'',
      result: [
        {
          id: 1,
          thumb: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnimg.ws.126.net%2F%3Furl%3Dhttp%253A%252F%252Fdingyue.ws.126.net%252F2021%252F0418%252F0b168191j00qrqg4a007hc000u0010ec.jpg%26thumbnail%3D650x2147483647%26quality%3D80%26type%3Djpg&refer=http%3A%2F%2Fnimg.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625125548&t=624013674d85140695e3916ad7de38b4',
          title: '你好-李焕英',
        },
        {
          id: 2,
          thumb: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.18183.com%2Fuploads%2Fallimg%2F210310%2F291-210310141310.jpg&refer=http%3A%2F%2Fimg.18183.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625126155&t=43e472815259932613352ca9d9eaca6c',
          title: '哥斯拉大战金刚',
        },
        {
          id: 3,
          thumb: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13181617797%2F641.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625126944&t=837bc262141e66aec9c2982a4a481f89',
          title: '刺杀小说家',
        },
        {
          id: 4,
          thumb: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.hebei.com.cn%2F003%2F025%2F724%2F00302572479_4b00c666.jpg&refer=http%3A%2F%2Fpic1.hebei.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625126581&t=9c56b3d3eb503e4bc9edf653ad30cd65',
          title: '我和我的家乡',
        },
        {
          id: 5,
          thumb: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1711004412,1210915593&fm=26&gp=0.jpg',
          title: '激战',
        },
        {
          id: 6,
          thumb: 'https://pic.rmb.bdstatic.com/926393c4862ffcc844e5a3ee1e3cd7d6.jpeg',
          title: '回到明朝当王爷',
        },
        
      ],
      showKeywords: false,
        keywords: ['山东肚脐橙', '湖南冰糖橙', '麻涌香蕉', '冰糖心苹果'],
        value:'',
        showResult: false,
  },
  cancelSearch() {
      this.setData({
        showResult: false,
        showKeywords: false,
        value:''
      })
  },
  searchInput(e) {
      if (!e.detail.value) {
        this.setData({
          showKeywords: false 
        })
      } else {
        if(!this.data.showKeywords) {
          timeId && clearTimeout(timeId);
          timeId = setTimeout(() => {
            this.setData({
              showKeywords: true,
            })
          }, 1000)
        }
      }
  },
keywordHandle(e) {
      const text = e.target.dataset.text;
      this.setData({
        value: text,
        showKeywords: false,
        showResult: true
      })
      this.historyHandle(text);
},
historyHandle(value) {
      let history = this.data.history;
      const idx = history.indexOf(value);
      if (idx === -1) {
        // 搜索记录只保留8个
        if (history.length > 7) {
          history.pop();
        }
      } else {
        history.splice(idx, 1);
      }
      history.unshift(value);
      wx.setStorageSync('history',JSON.stringify(history));
      this.setData({
          history
      });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
              const history = wx.setStorageSync('history');
              if (history) {
                this.setData({
                  history: JSON.parse(history)
                })
              console.log(this.data.history);
              }
              
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})